package com.dks.repository.impl.publicCompoment;

import com.dks.businessUtil.QsMailUtil;
import com.dks.dto.BaseIdRequestDto;
import com.dks.dto.publicCompoment.PublicCommentReplyDto;
import com.dks.mapper.PublicCommentMapper;
import com.dks.mapper.PublicCommentReplyMapper;
import com.dks.mapper.UsersMapper;
import com.dks.models.QsMail;
import com.dks.models.Users;
import com.dks.models.publicCompoment.PublicComment;
import com.dks.models.publicCompoment.PublicCommentReply;
import com.dks.repository.IPublicCommentReplyRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class PublicCommentReplyRepository implements IPublicCommentReplyRepository {

    @Autowired
    private PublicCommentReplyMapper publicCommentReplyMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private PublicCommentMapper publicCommentMapper;

    /**
     * 新增评论回复
     *
     * @author evan
     */
    @Override
    public ResultModel<PublicCommentReply> add(PublicCommentReplyDto publicCommentReplyDto) {
        PublicCommentReply publicCommentReply = new PublicCommentReply();
        publicCommentReply.setCommentId(publicCommentReplyDto.getCommentId());
        publicCommentReply.setReplyContent(publicCommentReplyDto.getReplyContent());
        publicCommentReply.setCreateUserId(publicCommentReplyDto.getUserId());

        Integer result = publicCommentReplyMapper.insert(publicCommentReply);
        if (result > 0) {
            PublicComment publicComment = publicCommentMapper.select(publicCommentReplyDto.getCommentId());
            if ("STUDENT_TASK".equals(publicComment.getCommentEntityType())) {
                //作业回复添加站内信
                Users users = usersMapper.select(publicCommentReplyDto.getUserId());
                String nickName = "";
                if (null != users) {
                    nickName = users.getNickname();
                }

                QsMail qsMail = null;
                if (0 == users.getUserType()) {
                    //家长类型
                    qsMail = new QsMail(publicCommentReplyDto.getUserId(), 7, nickName, "回复了您的评论，快去看看吧", publicCommentReplyDto.getCommentId());

                } else {
                    //老师类型
                    qsMail = new QsMail(publicCommentReplyDto.getUserId(), 5, nickName, "老师点评了您的作业，快去看看吧", publicCommentReplyDto.getCommentId());

                }
                QsMailUtil qsMailUtil = new QsMailUtil();
                qsMailUtil.insertQsMail(qsMail);
            }else if("TEACHER_DIARY".equals(publicComment.getCommentEntityType())){
                //教师日记回复添加站内信
                Users users = usersMapper.select(publicCommentReplyDto.getUserId());
                String nickName = "";
                if (null != users) {
                    nickName = users.getNickname();
                }
                QsMail qsMail = new QsMail(publicCommentReplyDto.getUserId(), 17, nickName, "回复了您的评论，快去看看吧", publicCommentReplyDto.getCommentId());
                QsMailUtil qsMailUtil = new QsMailUtil();
                qsMailUtil.insertQsMail(qsMail);
            }
            return Result.makeResult(ResultInfo.SUCCESS, publicCommentReplyMapper.select(publicCommentReply.getId()));
        } else {
            return Result.makeResult(ResultInfo.ERR_COMMENT_003);
        }
    }

    /**
     * 删除评论回复
     *
     * @author evan
     */
    @Override
    public ResultModel<PublicCommentReply> delete(BaseIdRequestDto baseIdRequestDto) {
        int result = publicCommentReplyMapper.delete(baseIdRequestDto.getId(), baseIdRequestDto.getUserId());
        if (result <= 0) {
            return Result.makeResult(ResultInfo.ERR_COMMENT_004, false);
        } else {
            return Result.makeResult(ResultInfo.SUCCESS, true);
        }
    }

}
